package com.beiding.controller;

import com.beiding.pojo.Room;
import com.beiding.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.security.Principal;
import java.util.Collection;

@Controller
public class IndexController {


    private RoomService roomService;

    @Autowired
    public void setRoomService(RoomService roomService) {
        this.roomService = roomService;
    }

    /*进入首页前判断用户是否同意用户协议*/
    @GetMapping("/")
    public String home(Principal principal) {

        UsernamePasswordAuthenticationToken user = (UsernamePasswordAuthenticationToken) principal;

        final Collection<GrantedAuthority> collection = user.getAuthorities();

        for (GrantedAuthority authority:collection)
        {
            //如果用户具有landlord权限则进入主页
            if (authority.getAuthority().equals("ROLE_TENANT")) {
                return "index";
            }
        }

        //如果没有指定权限,则进入协议页面,请用户同意相应的协议再继续
        return "agreement";

    }
    @GetMapping("/payment")
    String payment(String orderId, Model model) {
        model.addAttribute("orderId", orderId);
        return "payment";
    }

    @GetMapping("/refund.html")
    String refund(String orderId, boolean accept, Model model) {

        model.addAttribute("orderId", orderId);
        if (accept) {
            return "refundAccept";
        } else {
            return "refundNotAccept";
        }

    }


    @GetMapping("/forcedRefund.html")
    String forcedRefund(String orderId, Model model) {
        model.addAttribute("orderId", orderId);
        return "forcedRefund";

    }

    @GetMapping("/goto-payment.html")
    String goToPayment(String orderId, Model model) {
        model.addAttribute("orderId", orderId);
        return "goto-payment";

    }

    @GetMapping("/details.html")
    String details(String id, Model model) {
        Room room = roomService.get(id);
        model.addAttribute("room", room);
        return "details";
    }



}
